//
//  TripCityCell.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/23.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class TripCityCell: UITableViewCell {
    
    
    @IBOutlet weak var cityCollectionView: UICollectionView!
    @IBOutlet weak var cityLayout: UICollectionViewFlowLayout!
    
    @IBOutlet weak var viewCollectionView: UICollectionView!
    @IBOutlet weak var viewLayout: UICollectionViewFlowLayout!
    
    weak var superVC: UIViewController?

    private var forceReload: Bool = false
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.backgroundColor = kThemeBgColor
        self.cityCollectionView.backgroundColor = kThemeBgColor
        self.viewCollectionView.backgroundColor = kThemeBgColor
        
        self.cityCollectionView.register(UINib(nibName: "TripCityCCell", bundle: nil), forCellWithReuseIdentifier: "TripCityCCellCity")
        self.viewCollectionView.register(UINib(nibName: "TripCityCCell", bundle: nil), forCellWithReuseIdentifier: "TripCityCCellView")
        
        cityLayout.itemSize = CGSize(width: 54, height: 74)
        cityLayout.minimumLineSpacing = 20
        cityLayout.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 10, right: 20)
        
        viewLayout.itemSize = CGSize(width: 128, height: 128)
        viewLayout.minimumLineSpacing = 20
        viewLayout.sectionInset = UIEdgeInsets(top: 10, left: 20, bottom: 20, right: 20)
    }
    
    var selectIndex:Int = 0
    
    var citys:[City]? {
        
        willSet {
            
            if let new = newValue, let old = citys, new != old {
                
                forceReload = true
            } else {
                
                forceReload = false
            }
        }
        
        didSet {
            
            if forceReload {
                
                self.selectIndex = 0
                
                self.cityCollectionView.reloadData()
                self.viewCollectionView.reloadData()
                
                self.cityCollectionView.layoutIfNeeded()
                self.cityCollectionView.setContentOffset(CGPoint.zero, animated: false)
                
                self.viewCollectionView.layoutIfNeeded()
                self.viewCollectionView.setContentOffset(CGPoint.zero, animated: false)
            } else {
                
                self.cityCollectionView.reloadData()
                self.viewCollectionView.reloadData()
            }
        }
    }
}

extension TripCityCell: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        
        if collectionView == cityCollectionView {
            
            return citys?.count ?? 0
        }
        
        
        let city = citys?[selectIndex]
        
        return city?.featureCalendar?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if collectionView == cityCollectionView {
            
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TripCityCCellCity", for: indexPath) as! TripCityCCell
            cell.type = 1
            
            cell.city = citys?[indexPath.row]
            
            return cell
        }
        
        let city = citys?[selectIndex]
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TripCityCCellView", for: indexPath) as! TripCityCCell
        cell.type = 0
        
        cell.calendar = city?.featureCalendar?[indexPath.row]
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        
        if indexPath.row == self.selectIndex {
            
            collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .top)
            cell.isSelected = true
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if collectionView == cityCollectionView {
            
            self.selectIndex = indexPath.row
            self.viewCollectionView.reloadData()
            self.viewCollectionView.layoutIfNeeded()
            self.viewCollectionView.setContentOffset(CGPoint.zero, animated: false)
            return
        }
        
         let city = citys?[selectIndex]
        
        guard let calendar = city?.featureCalendar?[indexPath.row] else {
            
            return
        }
        
        if calendar.planType == .flight {
            
            let airVC = AirplaneDetailVC()
            airVC.viewmodel.calender = calendar
            self.superVC?.navigationController?.pushViewController(airVC, animated: true)
            
            return
        }
        
        let otherVC = OtherDetailVC()
        otherVC.viewmodel.calendar = calendar
        
        self.superVC?.navigationController?.pushViewController(otherVC, animated: true)
        
    }
    
}

